//
//  MyAccountModel.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class MyAccountModel: NSObject {
    
    static let shared = MyAccountModel()
    
    var accountArray = NSMutableArray()

    //将登陆状态传给闭包
    
    //外部闭包变量
    var comfun:((_ _data:NSMutableArray)->Void)?
    
    /**
     ## 登陆
     主要实现了登陆封装逻辑
     
     - tfNum    电话号码
     - tfPass   密码
     */
    func getAccout(_com:@escaping (_ _data:NSMutableArray)->Void) -> Void {
        

//        accountArray = [["b": "456", "a": "123", "c": "2010-10-09"], ["b": "456", "a": "123", "c": "2010-10-09"], ["b": "456", "a": "123", "c": "2010-10-09"]]
        
        comfun = _com
        
        
        guard let token = localSave.object(forKey: userToken) as? String else {
            return
        }

            //请求参数
            let params = ["token": token]

            print(params)
            
            //提示栏
            /**
         {
         “resultcode”: 0,
         “resultmsg”: “成功”,
         “data”:{
         “list”:[
         {	“reason”:” 奖励积分”,
         “result”:” +200”,
         “time”:”2016-10-11”},
         {	“reason”:” 兑换苹果Apple原装蓝牙耳机”,
         “result”:” -200”,
         “time”:”2016-10-12”}
         ]
         }
         
         }

            */
            postWithPath(path: integrallogURL, paras: params, success: { (response) in
                
                //判读返回值是否为空
                guard let dic = response as? NSDictionary else {
                    return
                }
                
                print(dic)
                
                guard let resultMsg = dic["resultmsg"] as? String else {
                    return
                }
                
                
                if resultMsg == "该账号已在异地登录，请重新登录" {
                    CustomAlertView.shared.alertWithTitle(strTitle: loginError)
                    
                    logoutModel.shared.logoutSEL(comfun: { (result) in
                        
                        print(result)
                        
                        DispatchQueue.main.async {
                            let mainVC = MainViewController()
                            mainVC.mvc = LoginView()
                            UIApplication.shared.keyWindow?.rootViewController = mainVC
                        }
                    })
                    
                }
                if resultMsg == "请求成功" {
                    print("请求成功",dic["data"])
                    
                    let dic2 = dic["data"] as! NSDictionary

                    
                    guard let array = dic2["list"] as? NSMutableArray else {
                        
                        return
                    }
                    
                    self.accountArray = array
                    
                    
                    //传值
                    self.comfun!(array)

                    //存入本地
                    localSave.set(array, forKey: jifenArray)
                    localSave.synchronize()

                    
                    
                }

                
            }, failure: { (error) in
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(error)
                
        
            })

    }
}
